#include <logbase.h>
#include <genrand.h>
#include <cycleQueHc.h>
#include <cstring.h>
#include <systime.h>
#pragma pack(1)
struct cqChkStc1
{
    int id;
    char name[16];
};
int cqChkCmpStc1(const void *a, const void *b)
{
    struct cqChkStc1 *tmpa = (struct cqChkStc1 *)a;
    struct cqChkStc1 *tmpb = (struct cqChkStc1 *)b;
    return tmpa->id - tmpb->id;
}
///////////////////////
struct cqChkStc
{
    int id;
    struct cstring name;
    struct cstring addr;
};
int cqChkAlloc(struct cqChkStc *tmpdt, int cfgval)
{
    // struct cqChkStc *tmpdt = (struct cqChkStc *)malloc(sizeof(struct cqChkStc));
    tmpdt->id = cfgval;
    tmpdt->name = cstrInitVal;
    tmpdt->addr = cstrInitVal;
    cstrSprint(&tmpdt->name, 32, "lxname-%d", tmpdt->id);
    cstrSprint(&tmpdt->addr, 32, "lxaddr-%d", tmpdt->id);
    logdebugc("alloc val:%d,%s,%s", tmpdt->id, tmpdt->name.str, tmpdt->addr.str);
    // return tmpdt;
    return 1;
}
void cqChkFree(struct cqChkStc *mydata)
{
    struct cqChkStc *tmpdt = (struct cqChkStc *)(mydata);
    // logdebugc("free val:%d,%s,%s", tmpdt->id, tmpdt->name.str, tmpdt->addr.str);
    cstrFree(&tmpdt->name);
    cstrFree(&tmpdt->addr);
    // free(tmpdt);
}
int cqChkCmpStc(const void *a, const void *b)
{
    struct cqChkStc *tmpa = (struct cqChkStc *)(a);
    struct cqChkStc *tmpb = (struct cqChkStc *)(b);
    return tmpa->id - tmpb->id;
}
#pragma pack()
///////////////////////
int cqChkCmpLong(const void *a, const void *b)
{
    long tmpa = *(long *)a;
    long tmpb = *(long *)b;
    return tmpa - tmpb;
}
///////////////////////
int cqChkCmpInt(const void *a, const void *b)
{
    int tmpa = *(int *)a;
    int tmpb = *(int *)b;
    return tmpa - tmpb;
}
int main(int argc, char **argv)
{
    cycleQueRoot tmpCqRoot;
    {
        cycleQueInit(&tmpCqRoot, NULL, sizeof(long), 1);
        for (long i = 0; i < 5; i++)
        {
            cycleQuePushBack(&tmpCqRoot, &i);
        }
        for (long i = 10; i < 15; i++)
        {
            cycleQuePushFront(&tmpCqRoot, &i);
        }
        cycleQueFree(&tmpCqRoot);
        while (1)
        {
            long tmpval;
            if (!cycleQuePullFront(&tmpCqRoot, &tmpval))
            {
                break;
            }
            logdebugc("get front:%ld", tmpval);
        }
        cycleQueFree(&tmpCqRoot);
    }
    {
        cycleQueInit(&tmpCqRoot, NULL, sizeof(long), 1);
        for (long i = 0; i < 5; i++)
        {
            cycleQuePushBack(&tmpCqRoot, &i);
        }
        for (long i = 10; i < 15; i++)
        {
            cycleQuePushFront(&tmpCqRoot, &i);
        }
        cycleQueFree(&tmpCqRoot);
        while (1)
        {
            long tmpval;
            if (!cycleQuePullBack(&tmpCqRoot, &tmpval))
            {
                break;
            }
            logdebugc("get back:%ld", tmpval);
        }
        cycleQueFree(&tmpCqRoot);
    }
    {
        cycleQueInit(&tmpCqRoot, cqChkCmpLong, sizeof(long), 1);
        for (long i = 0; i < 5; i++)
        {
            cycleQuePushBack(&tmpCqRoot, &i);
        }
        for (long i = 10; i < 15; i++)
        {
            cycleQuePushFront(&tmpCqRoot, &i);
        }
        cycleQueSort(&tmpCqRoot);
        long tmpfind = 10;
        long tmpFid = cycleQueSearch(&tmpCqRoot, &tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            long tmpval;
            if (!cycleQuePullFront(&tmpCqRoot, &tmpval))
            {
                break;
            }
            logdebugc("get front:%ld", tmpval);
        }
        cycleQueFree(&tmpCqRoot);
    }
    {
#define testSizeLen 1000000
        sysTmTp tmptmr;
        sysTmBeg(tmptmr);
        int *tmpbuf = (int *)malloc(testSizeLen * sizeof(int));
        for (int i = 0; i < testSizeLen; i++)
        {
            tmpbuf[i] = rand();
        }
        logdebugc("rand time:%ld", sysTmRunMs(tmptmr));
        sysTmBeg(tmptmr);
        cycleQueInit(&tmpCqRoot, cqChkCmpInt, sizeof(int), 1);
        for (int i = 0; i < testSizeLen; i++)
        {
            cycleQuePushBack(&tmpCqRoot, &tmpbuf[i]);
        }
        logdebugc("input time:%ld", sysTmRunMs(tmptmr));
        sysTmBeg(tmptmr);
        cycleQueSort(&tmpCqRoot);
        logdebugc("sort time:%ld", sysTmRunMs(tmptmr));
        int tmpfind = 10;
        size_t tmpFid = cycleQueSearch(&tmpCqRoot, &tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            int tmpval;
            if (!cycleQuePullFront(&tmpCqRoot, &tmpval))
            {
                break;
            }
            // logdebugc("get front:%ld", tmpval);
        }
        cycleQueFree(&tmpCqRoot);
        free(tmpbuf);
    }
    {
        cycleQueInit(&tmpCqRoot, cqChkCmpStc1, sizeof(struct cqChkStc1), 1);
        for (long i = 0; i < 5; i++)
        {
            struct cqChkStc1 tmpsv;
            tmpsv.id = i + 1;
            snprintf(tmpsv.name, sizeof(tmpsv.name), "lx-%d", tmpsv.id);
            cycleQuePushBack(&tmpCqRoot, &tmpsv);
        }
        for (long i = 10; i < 15; i++)
        {
            struct cqChkStc1 tmpsv;
            tmpsv.id = i + 1;
            snprintf(tmpsv.name, sizeof(tmpsv.name), "lx-%d", tmpsv.id);
            cycleQuePushFront(&tmpCqRoot, &tmpsv);
        }
        cycleQueSort(&tmpCqRoot);
        struct cqChkStc1 tmpfind;
        tmpfind.id = 12;
        long tmpFid = cycleQueSearch(&tmpCqRoot, &tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            struct cqChkStc1 tmpval;
            if (!cycleQuePullFront(&tmpCqRoot, &tmpval))
            {
                break;
            }
            logdebugc("get front:%d,n=%s", tmpval.id, tmpval.name);
        }
        cycleQueFree(&tmpCqRoot);
    }
    {
        cycleQueInit(&tmpCqRoot, cqChkCmpStc, sizeof(struct cqChkStc), 1);
        for (int i = 0; i < 5; i++)
        {
            struct cqChkStc tmpval;
            cqChkAlloc(&tmpval, i);
            cycleQuePushBack(&tmpCqRoot, &tmpval);
        }
        for (int i = 10; i < 15; i++)
        {
            struct cqChkStc tmpval;
            cqChkAlloc(&tmpval, i);
            cycleQuePushFront(&tmpCqRoot, &tmpval);
        }
        cycleQueSort(&tmpCqRoot);
        struct cqChkStc tmpfind;
        tmpfind.id = 10;
        long tmpFid = cycleQueSearch(&tmpCqRoot, &tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            struct cqChkStc tmpval;
            if (!(cycleQuePullFront(&tmpCqRoot, &tmpval)))
            {
                break;
            }
            logdebugc("get front:%d,%s,%s", tmpval.id, tmpval.name.str, tmpval.addr.str);
            cqChkFree(&tmpval);
        }
        cycleQueFree(&tmpCqRoot);
    }
    return 1;
}
